package com.ls.utils;

import com.alibaba.fastjson.JSON;
import com.ls.base.BaseResult;
import com.ls.exception.ErrorEnums;
import com.ls.exception.SysException;

/**
 * 返回对象
 **/
public class ResultData<T> {

    /** 返回码 */
    public static final int SUCCESS_CODE = 200;
    /** 多行错误返回码 */
    public static final int MUTI_ERROR_CODE = 444;
    /** 单行错误返回码 */
    public static final int COMMON_ERROR_CODE = 400;
    /** 错误返回码没有权限 */
    public static final int NO_AUTH_CODE = 402;

    private int code;

    private String msg;

    private T data;

    private boolean success;

    private String token;

    public boolean isSuccess() {
        return success;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public ResultData() {
        this.code = SUCCESS_CODE;
    }

    public static ResultData failure(String msg) {
        return new ResultData(COMMON_ERROR_CODE,msg);
    }
    public static ResultData noPermission(String msg) {
        return new ResultData(NO_AUTH_CODE,msg);
    }
    public static ResultData failure(int code , String msg) {
        return new ResultData(code,msg);
    }

    public static ResultData failure(ErrorEnums enums) {
        return new ResultData(enums.getCode(),enums.getMessage());
    }

    public static ResultData failure(ErrorEnums enums,String msg) {
        return new ResultData(enums.getCode(),enums.getMessage() + msg);
    }

    public static ResultData failData(Object data) {
        return new ResultData(MUTI_ERROR_CODE,"",data);
    }
    public static ResultData failMutiData(String msg) {
        return new ResultData(MUTI_ERROR_CODE,msg);
    }

    public static ResultData success() {
        return new ResultData(SUCCESS_CODE,"");
    }
    public static ResultData successMsg(String msg) {
        return new ResultData(SUCCESS_CODE,msg, null);
    }

    public static ResultData successData(Object data) {
        return new ResultData(SUCCESS_CODE,"", data);
    }

    public ResultData(int code, String msg) {
        this.code = code;
        this.msg = msg;
        if(code ==SUCCESS_CODE){
            this.success = true;
        }else{
            this.success = false;
        }
    }

    public ResultData(int code, String msg,  T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        if(code ==SUCCESS_CODE){
            this.success = true;
        }else{
            this.success = false;
        }
    }

    public boolean whetherSuccess(){
        if(this.code!=SUCCESS_CODE){
            return false;
        }
        return true;
    }
    public static ResultData convertBaseResultToResult(BaseResult baseResult){
        if(baseResult==null){
            return failure("ConvertBaseResultToResult BaseResult 涓?null ");
        }
        if(baseResult.getCode()!= SUCCESS_CODE){
            return failure(baseResult.getCode(),baseResult.getMessage());
        }else{
            return successData(baseResult.getData());
        }
    }

    public static ResultData error(SysException e) {
        return new ResultData(e.getCode(), e.getMessage(), null);
    }

    public static ResultData convertFromStr(String str ){
        ResultData resultData = JSON.parseObject(str, ResultData.class);
        return resultData;
    }
}
